package com.jiuyin.dianjing.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.jiuyin.dianjing.R;

import java.util.List;

import static com.jiuyin.dianjing.util.DisplayUtil.dp2px;

//todo 继续理一理逻辑

public class MatchStatusView extends View {
    private static final float DEFAULT_ICON_SIZE = 14f;
    private static final float DEFAULT_PROGRESS_WIDTH = 14f;
    private int mWidth;
    private int mHeight;
    private int cellWidth;
    private int iconSize;
    private int textSize;
    private int textColor;
    private int textColorcheked;
    private int progressWidth;
    private int uncheckedProgressColor = Color.GRAY;
    private int checkedProgressColor = Color.YELLOW;
    private int marginLeft = 100;
    private int marginRight = 100;
    private int lineStart = 20;
    private int lineEnd = 20;
    private int lineLeft;
    private Rect iconRect = new Rect();
    private Rect textRectF = new Rect();
    private int ALL_POINT_SIZE = 4;
    private int SELECT_POINT_SIZE;
    private List<String> mTime;
    private List<String> mStatus;
    private Bitmap iconUncheckedBitmapRes;
    private Bitmap iconCheckedBitmapRes;
    private int margin;
    private Paint iconPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint linePaint = new Paint();
    private Paint textPaint = new Paint();

    public MatchStatusView(Context context) {
        super(context);
    }


    public MatchStatusView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MatchStatusView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StudyPlanProgressView);
        iconUncheckedBitmapRes = ((BitmapDrawable) (typedArray.getDrawable(R.styleable.StudyPlanProgressView_sppv_iconUnchecked))).getBitmap();
        iconCheckedBitmapRes = ((BitmapDrawable) (typedArray.getDrawable(R.styleable.StudyPlanProgressView_sppv_iconChecked))).getBitmap();
        iconSize = typedArray.getDimensionPixelOffset(R.styleable.StudyPlanProgressView_sppv_iconSize, dp2px(getContext(), DEFAULT_ICON_SIZE));
        progressWidth = typedArray.getDimensionPixelOffset(R.styleable.StudyPlanProgressView_sppv_progressWidth, dp2px(getContext(), DEFAULT_PROGRESS_WIDTH));
        uncheckedProgressColor = typedArray.getColor(R.styleable.StudyPlanProgressView_sppv_uncheckedProgressColor, uncheckedProgressColor);
        checkedProgressColor = typedArray.getColor(R.styleable.StudyPlanProgressView_sppv_checkedProgressColor, checkedProgressColor);
        textSize = typedArray.getDimensionPixelSize(R.styleable.StudyPlanProgressView_sppv_textSize, dp2px(getContext(), DEFAULT_PROGRESS_WIDTH));
        textColor = typedArray.getColor(R.styleable.StudyPlanProgressView_sppv_textColor, checkedProgressColor);
        typedArray.recycle();
        margin = marginLeft + marginRight;
        initPaint();
    }

    private void initPaint() {
        iconPaint.setDither(true);
        iconPaint.setStrokeWidth(progressWidth);
        iconPaint.setFilterBitmap(true);

        linePaint.setStrokeWidth(progressWidth);
        linePaint.setAntiAlias(true);
        linePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        linePaint.setColor(uncheckedProgressColor);

        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(textSize);
        textPaint.setColor(uncheckedProgressColor);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        cellWidth = (mWidth - margin - lineStart - lineEnd - iconSize * ALL_POINT_SIZE) / (ALL_POINT_SIZE - 1);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        cellWidth = (mWidth - margin - lineStart - lineEnd - iconSize * ALL_POINT_SIZE) / (ALL_POINT_SIZE - 1);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawProgressLine(canvas);
        drawProgress(canvas);
    }

    private void drawProgressLine(Canvas canvas) {
        canvas.drawLine(marginLeft, mHeight / 2, mWidth - margin, mHeight / 2, linePaint);
        int size = getDataSize();
        int end = marginLeft + (size - 1) * (cellWidth + iconSize) + lineStart;
        linePaint.setColor(checkedProgressColor);
        canvas.drawLine(marginLeft, mHeight / 2, 1, end, linePaint);
    }

    private void drawProgress(Canvas canvas) {
        if (iconUncheckedBitmapRes == null || iconCheckedBitmapRes == null) {
            return;
        }
        for (int i = 0; i < ALL_POINT_SIZE; i++) {
            int left = marginLeft + lineStart + (cellWidth) * i;
            iconRect.set(left, (mHeight - iconSize) / 2, left + iconSize, (mHeight + iconSize) / 2);
            canvas.drawBitmap(iconUncheckedBitmapRes, null, iconRect, iconPaint);
            drawText(canvas, i);

        }
//        textPaint.setColor(checkedProgressColor);
//        for (int i = SELECT_POINT_SIZE; i < ALL_POINT_SIZE; i++) {
//            int left = marginLeft + lineStart + marginRight + (cellWidth + iconSize) * i;
//            iconRect.set(left, (mHeight - iconSize) >> 1, left + iconSize, (mHeight + iconSize) >> 1);
//            canvas.drawBitmap(iconCheckedBitmapRes, null, iconRect, iconPaint);
//            drawText(canvas, i);
//        }
    }

    private void drawText(Canvas canvas, int position) {
        if (getDataSize() == 0) return;
        if (position < getDataSize()) {
            int textWidth = (int) textPaint.measureText(mStatus.get(position));
            int left = marginLeft + marginRight + iconSize / 2 + (cellWidth + iconSize) * position - textWidth / 2;
            textRectF.left = left;
            textRectF.right = left + textWidth;
            textRectF.top = (mHeight + iconSize) / 2;
            textRectF.bottom = mHeight;
            Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
            int baseline = (textRectF.bottom + textRectF.top - fontMetrics.bottom - fontMetrics.top) / 2;
            //文字绘制到整个布局的中心位置
            canvas.drawText(mStatus.get(position), textRectF.centerX(), baseline, textPaint);
        }
    }

    private int getDataSize() {
        return mStatus.size();
    }

    public void setData(List<String> statusList, List<String> timeList) {
        if (null == statusList || null == timeList) {
            throw new RuntimeException();
        }
        int ALL_POINT_SIZE = statusList.size();
        int SELECT_POINT_SIZE = timeList.size();
        if (0 > ALL_POINT_SIZE || 0 > SELECT_POINT_SIZE || SELECT_POINT_SIZE > ALL_POINT_SIZE) {
            throw new RuntimeException();
        }

        if (ALL_POINT_SIZE < SELECT_POINT_SIZE) {
            throw new RuntimeException();
        }
        mTime = timeList;
        mStatus = statusList;
        invalidate();
    }
}
